package fhp.hlhj.giantfold.activity.user

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.widget.Toast
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.event.RefreshLoginEvent
import fhp.hlhj.giantfold.interfaces.IBindPhone
import fhp.hlhj.giantfold.interfaces.IPhoneLogin
import fhp.hlhj.giantfold.javaBean.BindPhoneBean
import fhp.hlhj.giantfold.javaBean.GetSmsBean
import fhp.hlhj.giantfold.javaBean.PhoneLoginBean
import fhp.hlhj.giantfold.presenter.BindPhonePresenter
import fhp.hlhj.giantfold.presenter.PhoneLoginPresenter
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.activity_bind_phone_aty.*
import org.greenrobot.eventbus.EventBus

class BindPhoneAty : BaseActivity(),IBindPhone ,IPhoneLogin{

    private var sec = 60
    private val handler = @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            btGetYzm.isClickable=false
            sec--
            btGetYzm.text=sec.toString()+"秒"
            if (sec<1){
                btGetYzm.text="获取验证码"
                btGetYzm.isClickable=true
            }else{
                this.sendEmptyMessageDelayed(1,1000)
            }
        }
    }
    private lateinit var phoneLoginPresenter: PhoneLoginPresenter
    private lateinit var presenter:BindPhonePresenter

    override fun getContentId(): Int = R.layout.activity_bind_phone_aty


    override fun initView() {
        presenter= BindPhonePresenter(this)
        phoneLoginPresenter= PhoneLoginPresenter(this)
    }

    override fun initListener() {

        btGetYzm.setOnClickListener {
            if (etPhone.text.isEmpty()){
                MyUtils.toast("手机号不正确")
            }else{
                val params = HttpParams()
                params.put("phone",etPhone.text.toString())
                params.put("sms_key","47ec2dd791e31e2ef2076caf64ed9b3d")
                params.put("sms_type",2)
                params.put("client","android")

                presenter.getSms(params)
            }
        }

        btCommit.setOnClickListener {
            if (etPhone.text.isEmpty()){
                MyUtils.toast("手机号不正确")
            }else{
                if (etYzm.text.isEmpty()){
                    MyUtils.toast("验证码为空")
                }else{
                    if (etNewPwd.text.isEmpty()||etnewPwdAg.text.isEmpty()){
                        MyUtils.toast("密码不能为空")
                    }else{
                        if (!etNewPwd.text.toString() .equals(etnewPwdAg.text.toString()) ){
                            MyUtils.toast("两次输入的密码不一致")
                        }else{
                            MyUtils.toast("登陆形式${SPUtils.readLoginType(this)}")
                            when(SPUtils.readLoginType(this)){
                                1->{
                                    val params = HttpParams()
                                    params.put("phone",etPhone.text.toString())
                                    params.put("sms_code",etYzm.text.toString())
                                    params.put("password",etNewPwd.text.toString())
                                    params.put("type",0)
                                    params.put("openid",SPUtils.readOpenId(this))
                                    presenter.bindPhone(params)
                                }
                            }
                        }

                    }
                }
            }
        }

    }
    override fun showLoading() {
        loadingDialog.show()
    }

    override fun bindPhone(bindPhoneBean: BindPhoneBean) {
        loadingDialog.dismiss()
        if (bindPhoneBean.code==200){
            MyUtils.toast("绑定成功")
            val params = HttpParams()
            params.put("user_name",etPhone.text.toString())
            params.put("password",etNewPwd.text.toString())
            phoneLoginPresenter.login(params)
        }else{
            MyUtils.toast(bindPhoneBean.msg)
        }

    }
    override fun getSmsCode(getSmsBean: GetSmsBean) {
        loadingDialog.dismiss()
        if (getSmsBean.code==200){
            MyUtils.toast("获取验证码成功")
            handler.sendEmptyMessage(1)
        }else{
            MyUtils.toast(getSmsBean.msg)
        }
    }

    /**
     * 绑定成功之后刷新登陆
     */
    override fun login(phoneLoginBean: PhoneLoginBean) {
        if (phoneLoginBean.code==200){
            Toast.makeText(this,"登录成功", Toast.LENGTH_SHORT).show()
            Contents.isLogin=true
            Contents.userInfo=phoneLoginBean.data
            Contents.userId=phoneLoginBean.data.id
            startActivity(Intent(this, MainActivity::class.java))
            EventBus.getDefault().postSticky(RefreshLoginEvent())
            SPUtils.saveUserIsLogin(this,true)
            SPUtils.saveUser(this,etPhone.text.toString(),etnewPwdAg.text.toString())
            SPUtils.saveLoginType(this,0)
            finish()
        }
    }


}
